package com.cc.dao.impl;

import com.cc.dao.BaseDao;
import com.cc.dao.EmployeeDao;
import com.cc.entity.Employee;
import com.cc.entity.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CC
 * @version 1.0
 * @description: TODO
 * @date 2025/4/18 8:59
 */
public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {
    @Override
    public Page<Employee> getEmployeeList(Page<Employee> page, String empName) {

        List<Employee> employeeList = new ArrayList<>();

        //sql指令
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT e.*,d.dept_name deptName ");
        sb.append(" FROM employee e ,department d ");
        sb.append(" WHERE e.dep_id = d.id ");
        if (empName != null && !"".equals(empName)) {
            sb.append(" AND e.emp_name LIKE '%" + empName + "%' ");
        }
        sb.append(" ORDER BY e.id DESC ");
        sb.append(" LIMIT " + (page.getPageNum() - 1) * page.getPageSize() + "," + page.getPageSize());
        //执行sql
        ResultSet rs = executeQuery(sb.toString());
        if (rs != null) {
            try {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setEmpName(rs.getString("emp_name"));
                    employee.setDepId(rs.getInt("dep_id"));
                    employee.setDepName(rs.getString("deptName"));
                    employee.setPosition(rs.getString("position"));
                    employee.setStartDate(rs.getDate("start_date"));
                    employee.setPhone(rs.getString("phone"));
                    employee.setImgUrl(rs.getString("img_url"));
                    employeeList.add(employee);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        //设置数据
        page.setData(employeeList);
        page.setTotal(getEmployeeCount(empName));
        return page;
    }

    @Override
    public int getEmployeeCount(String empName) {
        int count = 0;
        //sql指令
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT count(1) count ");
        sb.append(" FROM employee e ,department d ");
        sb.append(" WHERE e.dep_id = d.id ");
        if (empName != null && !"".equals(empName)) {
            sb.append(" AND e.emp_name LIKE '%" + empName + "%' ");
        }
        ResultSet rs = executeQuery(sb.toString());
        if (rs != null) {
            try {
                while (rs.next()) {
                    count = rs.getInt("count");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return count;
    }

    @Override
    public int update(Employee employee) {
        String sql = "UPDATE employee SET emp_name=?,dep_id=?,position=?,start_date=?,phone=?,img_url=? WHERE id=?";
        int count = executeUpdate(sql,
                employee.getEmpName(),
                employee.getDepId(),
                employee.getPosition(),
                employee.getStartDate(),
                employee.getPhone(),
                employee.getImgUrl(),
                employee.getId());
        return count;
    }

    @Override
    public int insert(Employee employee) {
        String sql = "INSERT INTO employee(emp_name,dep_id,position,start_date,phone,img_url) VALUES(?,?,?,?,?,?)";
        int count = executeUpdate(sql,
                employee.getEmpName(),
                employee.getDepId(),
                employee.getPosition(),
                employee.getStartDate(),
                employee.getPhone(),
                employee.getImgUrl());
        return count;
    }
}
